package com.imooc.business.util.report;


import com.imooc.business.util.report.vo.ReportItem;
import com.imooc.business.util.report.vo.ReportParam;
import com.imooc.module.collect.enums.PeriodEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 报表工具类
 */
public class ReportUtil {

	/**
	 * 获取天入口
	 *
	 * @param maxTime
	 * @param size
	 * @return
	 */
	public static List<ReportItem> generateReportDayItems(Date minTime, Date maxTime, int size, boolean isEntry) {
		List<ReportItem> items = new ArrayList<ReportItem>();
		ReportItem item = null;
		ReportParam reportParam = null;

		Date time = maxTime;
		for (int i = 0; i < size; i++) {
			item = new ReportItem();
			if (isEntry) {
				item.setName(PeriodEnum.DAY.getDescription());
			}else {
				item.setName(DateUtil.format(time, DateUtil.MM_DD_ZH_CN));
			}
			item.setFullName(item.getName());
			item.setClickAble(true);

			reportParam = new ReportParam();
			reportParam.setStartDate(DateUtil.format(time, DateUtil.YYYYMMDD));
			reportParam.setEndDate(DateUtil.format(time, DateUtil.YYYYMMDD));
			reportParam.setTimeType(PeriodEnum.DAY.getType());

			item.setParams(reportParam);
			item.setDescription("0");
			items.add(item);

			//超出时段  退出
			if(time.before(minTime)) {
				break;
			}

			time = DateUtil.getBeforeDate(maxTime, i + 1);

		}
		return items;
	}

	private static ReportItem createDayItem(Date time){
		ReportItem item = new ReportItem();
		item.setName(DateUtil.format(time, DateUtil.MM_DD_ZH_CN));
		item.setFullName(item.getName());
		item.setClickAble(true);

		ReportParam reportParam = new ReportParam();
		reportParam.setStartDate(DateUtil.format(time, DateUtil.YYYYMMDD));
		reportParam.setEndDate(DateUtil.format(time, DateUtil.YYYYMMDD));
		reportParam.setTimeType(PeriodEnum.DAY.getType());
		item.setParams(reportParam);
		return item;
	}

	/**
	 * 生成周入口
	 *
	 * @param maxTime
	 * @param size
	 * @return
	 */
	public static List<ReportItem> generateReportWeekItems(Date minTime, Date maxTime, int size, boolean isEntry) {
		List<ReportItem> items = new ArrayList<ReportItem>();
		ReportItem item = null;
		ReportParam reportParam = null;

		Date week = maxTime;
		for (int i = 0; i < size; i++) {
			Date weekBeginDate = DateUtil.getWeekBeginDay(week);
			Date weekEndDate = DateUtil.getWeekEndDay(week);

			item = new ReportItem();
			if (isEntry){
				item.setName(PeriodEnum.WEEK.getDescription());
			}else {
				item.setName(DateUtil.format(weekBeginDate, "MM月dd").concat("-").concat(DateUtil.format(weekEndDate, "dd日")));
			}
			item.setFullName(item.getName());
			item.setClickAble(true);

			reportParam = new ReportParam();
			reportParam.setStartDate(DateUtil.format(weekBeginDate, DateUtil.YYYYMMDD));
			reportParam.setEndDate(DateUtil.format(weekEndDate, DateUtil.YYYYMMDD));
			reportParam.setTimeType(PeriodEnum.WEEK.getType());
			item.setParams(reportParam);

			items.add(item);
			week = DateUtil.getAfterDate(week, -7);
			if(weekBeginDate.before(minTime)){
				break;
			}
		}
		return items;
	}

	/**
	 * 生成月入口
	 *
	 * @param maxTime
	 * @param size
	 * @return
	 */
	public static List<ReportItem> generateReportMonthItems(Date minTime, Date maxTime, int size, boolean isEntry) {
		List<ReportItem> items = new ArrayList<ReportItem>();
		ReportItem item = null;
		ReportParam reportParam = null;

		Date month = maxTime;
		for (int i = 0; i < size; i++) {
			Date monthBeginDate = DateUtil.getMonthBeginDay(month);
			Date monthEndDate = DateUtil.getMonthEndDay(month);

			item = new ReportItem();
			if (isEntry){
				item.setName(PeriodEnum.MONTH.getDescription());
			}else {
				item.setName(DateUtil.format(monthBeginDate, "yy年MM月"));
			}
			item.setFullName(item.getName());
			item.setClickAble(true);

			reportParam = new ReportParam();
			reportParam.setStartDate(DateUtil.format(monthBeginDate, DateUtil.YYYYMMDD));
			reportParam.setEndDate(DateUtil.format(monthEndDate, DateUtil.YYYYMMDD));
			reportParam.setTimeType(PeriodEnum.MONTH.getType());
			item.setParams(reportParam);

			items.add(item);
			month = DateUtil.getBackMonth(month);

			if(monthBeginDate.before(minTime)){
				break;
			}
		}
		return items;
	}

	/**
	 * 生成学期入口
	 *
	 * @param maxTime
	 * @param size
	 * @return
	 */
	public static List<ReportItem> generateReportTermItems(Date minTime, Date maxTime, int size, boolean isEntry) {
		List<ReportItem> items = new ArrayList<ReportItem>();
		ReportItem item = null;
		ReportParam reportParam = null;

		Date tempTime = maxTime;
		String termStartDate = null, termEndDate = null;
		for (int i = 0; i < size; i++) {
			termStartDate = SchoolYearUtil.getSeasonStartDate(tempTime);
			termEndDate = SchoolYearUtil.getSeasonEndDate(tempTime);

			item = new ReportItem();
			String name = SchoolYearUtil.getSchoolYearName(tempTime);
			if(name==null) {
				continue;
			}
			String simpleName = name.replace(SchoolYearUtil.FIRST_SCHOOL_YEAR, "上");
			simpleName = simpleName.replace(SchoolYearUtil.SECOND_SCHOOL_YEAR, "下");
			if (isEntry){
				item.setName(PeriodEnum.SEMESTER.getDescription());
			}else{
				item.setName(simpleName);
			}
			item.setFullName(item.getName());
			item.setClickAble(true);

			reportParam = new ReportParam();
			reportParam.setStartDate(DateUtil.format(DateUtil.parse(termStartDate, DateUtil.YYYY_MM_DD), DateUtil.YYYYMMDD));
			reportParam.setEndDate(DateUtil.format(DateUtil.parse(termEndDate, DateUtil.YYYY_MM_DD), DateUtil.YYYYMMDD));
			reportParam.setTimeType(PeriodEnum.SEMESTER.getType());
			item.setParams(reportParam);

			items.add(item);
			tempTime = DateUtil.getBackMonth(DateUtil.parse(termStartDate, DateUtil.YYYY_MM_DD));

			if(DateUtil.parse(termStartDate, DateUtil.YYYY_MM_DD).before(minTime)){
				break;
			}
		}
		return items;
	}

	/**
	 * 生成学年入口
	 *
	 * @param maxTime
	 * @param size
	 * @return
	 */
	public static List<ReportItem> generateReportYearItems(Date minTime, Date maxTime, int size, boolean isEntry) {
		List<ReportItem> items = new ArrayList<ReportItem>();
		ReportItem item = null;
		ReportParam reportParam = null;

		Date tempTime = maxTime;
		String year = null;
		Date yearStartDate = null, yearEndDate = null;
		for (int i = 0; i < size; i++) {
			year = SchoolYearUtil.getSchoolYear(tempTime);
			yearStartDate = SchoolYearUtil.getStartDate(year);
			yearEndDate = SchoolYearUtil.getEndDate(year);

			item = new ReportItem();
			String name = SchoolYearUtil.getSchoolYear(tempTime);
			if (isEntry) {
				item.setName(PeriodEnum.SCHOOL_YEAR.getDescription());
				item.setFullName(item.getName());
			}else {
				item.setName(name.substring(2) + "学年");
				item.setFullName(name + "学年");
			}
			item.setClickAble(true);

			reportParam = new ReportParam();
			reportParam.setStartDate(DateUtil.format(yearStartDate, DateUtil.YYYYMMDD));
			reportParam.setEndDate(DateUtil.format(yearEndDate, DateUtil.YYYYMMDD));
			reportParam.setTimeType(PeriodEnum.SCHOOL_YEAR.getType());
			item.setParams(reportParam);

			items.add(item);
			tempTime = DateUtil.getBeforeMonth(tempTime, 12);

			if(yearStartDate.before(minTime)){
				break;
			}
		}
		return items;
	}

	/**
	 * 获取入口名称
	 *
	 * @param timeType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getEntryName(Integer timeType, Date startDate, Date endDate) {
		if (Objects.isNull(timeType) || (startDate == null) || (endDate == null)) {
			return null;
		}

		if (PeriodEnum.DAY.getType() == timeType) {
			return DateUtil.format(startDate, DateUtil.MM_DD_ZH_CN);
		} else if (PeriodEnum.WEEK.getType() == timeType) {
			return DateUtil.format(startDate, "MM月dd").concat("-").concat(DateUtil.format(endDate, "dd日"));
		} else if (PeriodEnum.MONTH.getType() == timeType) {
			return DateUtil.format(startDate, "yy年MM月");
		} else if (PeriodEnum.SEMESTER.getType() == timeType) {
			String name = SchoolYearUtil.getSchoolYearName(startDate);
			name = name.replace(SchoolYearUtil.FIRST_SCHOOL_YEAR, "上");
			name = name.replace(SchoolYearUtil.SECOND_SCHOOL_YEAR, "下");
			if (name.indexOf("-") > 0) {
				String[] years = name.split("-");
				name = years[0].substring(2) + "-" + years[1].substring(2);
			}
			return name;
		} else if (PeriodEnum.SCHOOL_YEAR.getType() == timeType) {
			String name = SchoolYearUtil.getSchoolYear(startDate);
			name = name.substring(2);
			return name + "学年";
		} else {
			return null;
		}
	}
}
